package com.iceclean.daoImpl;

import com.iceclean.dao.PostCollectedDao;
import com.iceclean.po.PostBrowsing;
import com.iceclean.po.PostCollected;
import com.iceclean.util.JdbcUtil;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-05
 *
 * 贴子收藏表操作实现类
 */
public class PostCollectedDaoImpl implements PostCollectedDao {
    @Override
    public PostCollected findPostCollected(int postId, int userId) {
        List<PostCollected> postCollects = JdbcUtil.query("select * from t_post_collected where post_id = ? and user_id = ?", PostCollected.class, postId, userId);
        return postCollects.size() == 0 ? null : postCollects.get(0);
    }

    @Override
    public PostCollected findPostCollectedById(int postCollectedId) {
        List<PostCollected> postCollects = JdbcUtil.query("select * from t_post_collected where collected_id = ?", PostCollected.class, postCollectedId);
        return postCollects.size() == 0 ? null : postCollects.get(0);
    }

    @Override
    public List<PostCollected> findPostCollectedByPage(int userId, int page, int num) {
        List<PostCollected> postCollectedList = JdbcUtil.query("select * from t_post_collected where user_id = ? limit " + (page-1)*num + "," + num, PostCollected.class, userId);
        return postCollectedList.size() == 0 ? null : postCollectedList;
    }

    @Override
    public int getNumberOfAllPostCollected(int userId) {
        return JdbcUtil.query("select collected_id from t_post_collected where user_id = ?", PostCollected.class, userId).size();
    }

    @Override
    public int createPostCollected(PostCollected postCollected) {
        return JdbcUtil.insert("t_post_collected", postCollected);
    }

    @Override
    public boolean cancelPostCollected(PostCollected postCollected) {
        return JdbcUtil.delete("t_post_collected", "collected_id=" + postCollected.getCollectedId());
    }
}
